package com.x52im.rainbowchat.http.logic.pay.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.eva.framework.utils.LoggerFactory;
import com.x52im.rainbowchat.BaseConf;
import org.apache.commons.lang.RandomStringUtils;

public class AlipayClientTools {
    private static AlipayClientTools INSTANCE;

    public static AlipayClient alipayClient = null;

    private static synchronized void createAlipayClient() {
        long start = System.currentTimeMillis();
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
        certAlipayRequest.setAppId(BaseConf.getInstance().getAPP_ID());
        certAlipayRequest.setPrivateKey(BaseConf.getInstance().getAPP_privateKey());
        certAlipayRequest.setFormat("json");
        certAlipayRequest.setCharset("UTF-8");
        certAlipayRequest.setSignType("RSA2");
        //设置应用公钥证书路径
        certAlipayRequest.setCertPath(BaseConf.getInstance().getAPP_certPublicKey());
        //设置支付宝公钥证书路径
        certAlipayRequest.setAlipayPublicCertPath(BaseConf.getInstance().getALI_certPublicKey());
        //设置支付宝根证书路径
        certAlipayRequest.setRootCertPath(BaseConf.getInstance().getALI_rootPath());
        try {
            AlipayClientTools.alipayClient = new DefaultAlipayClient(certAlipayRequest);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        LoggerFactory.getLog().debug("初始化AlipayClientTools耗费:" + ((end - start) / 1000) + " 秒");
    }

    public static AlipayClientTools getInstance() {
        if (INSTANCE == null) {
            synchronized (AlipayClientTools.class) {
                if (INSTANCE == null) {
                    INSTANCE = new AlipayClientTools();
                }
            }
        }

        if (alipayClient == null) {
            createAlipayClient();
        }

        return INSTANCE;
    }

    /**
     * 充值,获取支付外部订单编号
     *
     * @return
     */
    public static String getOutTradeNo(String uid) {
        String prefix = "ALAPPZF_" + uid + "_";
        String nano = String.valueOf(System.nanoTime());
        String rnd = RandomStringUtils.randomAlphanumeric(20);
        String no = String.format("%s%s%s", prefix, nano, rnd);
        return no;
    }

    /**
     * 实名授权码,获取目标用户授权的订单编号
     *
     * @return
     */
    public static String getTargetId(String uid) {
        String prefix = "ALAPPAUTH_" + uid + "_";
        String nano = String.valueOf(System.nanoTime());
        String rnd = RandomStringUtils.randomAlphanumeric(5);
        String no = String.format("%s%s%s", prefix, nano, rnd);
        return no;
    }

    /**
     * 提现,获取外部订单标识
     *
     * @param uid
     * @return
     */
    public static String getOutBizNo(String uid) {
        String prefix = "ALAPPTX_" + uid + "_";
        String nano = String.valueOf(System.nanoTime());
        String rnd = RandomStringUtils.randomAlphanumeric(20);
        String no = String.format("%s%s%s", prefix, nano, rnd);
        return no;
    }


}
